<?php
/**
 * 创建模版文件
 */
namespace plugins\qbt_custom_model\service;
use think\Db;

class CreateHtmlService{
    /**
     * 创建添加模版
     */
    public static function create_add_tpl($model_info){
        $path = str_replace("\\",DS,dirname(__DIR__)).DS.'data'.DS.'tpl'.DS.'add_view.tpl';
        $name = $model_info['model_controller'];
        //取出下面所有字段信息
        $field_info = Db::name("model_field")->where(["model_id"=>$model_info['model_id'],"field_add_show"=>1])->order("field_sort asc")->select()->toArray();
       // var_dump($field_info);exit;
        //获取html
        $html = '';
        $html_many_column = [];
        #nsh 20190107 修改
        if($model_info['distinguish']==1){
            $html.=' <tr><th for="input-parent">上级</th>
                            <td>
                                <select class="form-control" name="parent_id" id="input-parent">
                                    <option value="0">作为一级分类</option>
                                    {$categories_tree}
                                </select>
                            </td>
                        </tr>';
        }
        $html_service = new HtmlService();
        foreach ($field_info as $k=>$v){
            if($field_info[$k]['field_add_edit_show_many_column']){
                $html_many_column[]= $html_service->get_html($v['field_type'],$field_info[$k]);
            }else{
                $html .= $html_service->get_html($v['field_type'],$field_info[$k]);
            }
        }
       // var_dump($html_many_column);exit;
//        $html_arr = explode('</tr>',$html_many_column);
        $h_a=[];
        foreach ($html_many_column as $html_arr_val){
            $html_arr_str=str_replace(array('<tr>','</tr>'),array('',''),$html_arr_val);
            if(!empty($html_arr_str)){
                $h_a[]=$html_arr_str;
            }
        }
        $str='';
        if(!empty($h_a)){
            $str = getArrayAssemble($h_a);
        }
//        exit;
//        var_dump();
        $html_str=  substr_replace(str_replace('<td','<td colspan="3"',$html), $str, 0, 0);
        //文本信息
        $desc = rtrim($model_info["model_name"],"模型");

        //获取js模板
        $add_photo_js = TplService::get_photo_js($field_info);
        $add_files_js = TplService::get_files_js($field_info);


        $content  = file_get_contents($path);
        $str = preg_replace("/([A-Z])/","_\\1",$name);
        //去除第一个_
        $str = strtolower(ltrim($str,"_"));
        $filedir = CMF_ROOT."public/themes/admin_simpleboot3/admin/".$str."/";
        Directory($filedir);
        $fileName = $filedir."add.html";
        //开始替换
        ob_start();
        echo str_replace(array("#CONTROLLERNAME#","#replace_content#","#ADD_PHOTO_JS#","#ADD_FILES_JS#","#DESC#"),array($name,$html_str,$add_photo_js,$add_files_js,$desc),$content);
        $info2 = ob_get_contents();
        file_put_contents($fileName,$info2);
        ob_end_clean();
    }

    /**
     * 创建编辑模版
     */
    public static function create_edit_tpl($model_info){
        $path = str_replace("\\",DS,dirname(__DIR__)).DS.'data'.DS.'tpl'.DS.'edit_view.tpl';
        $name = $model_info['model_controller'];
        //取出下面所有字段信息
        $field_info = Db::name("model_field")->where(["model_id"=>$model_info['model_id'],"field_edit_show"=>1])->order("field_sort asc")->select()->toArray();

        //获取html
        $html = '';
        $html_many_column = '';
        #nsh 20190107 修改
        if($model_info['distinguish']==1){
            $html.=' <tr><th for="input-parent">上级</th>
                            <td>
                                <select class="form-control" name="parent_id" id="input-parent">
                                    <option value="0">作为一级分类</option>
                                    {$categories_tree}
                                </select>
                            </td>
                        </tr>';
        }
        $html_service = new HtmlEditService();
        foreach ($field_info as $k=>$v){
            if($field_info[$k]['field_add_edit_show_many_column']){
                $html_many_column .= $html_service->get_html($v['field_type'],$field_info[$k]);
            }else{
                $html .= $html_service->get_html($v['field_type'],$field_info[$k]);
            }
        }
        $html_arr = explode('</tr>',$html_many_column);
        $h_a=[];
        foreach ($html_arr as $html_arr_val){
//            $html_arr_str=str_replace('<tr>','',$html_arr_val);
            $html_arr_str=str_replace(array('<tr>','</tr>'),array('',''),$html_arr_val);
            if(!empty($html_arr_str)){
                $h_a[]=$html_arr_str;
            }
        }
        $str='';
        if(!empty($h_a)){
            $str = getArrayAssemble($h_a);
        }
        $html_str=  substr_replace(str_replace('<td','<td colspan="3"',$html), $str, 0, 0);
//        echo $html_str;exit;
        //文本信息
        $desc = rtrim($model_info["model_name"],"模型");

        //获取js模板
        $edit_photo_js = TplService::get_photo_js($field_info);
        $edit_files_js = TplService::get_files_js($field_info);


        $content  = file_get_contents($path);
        $str = preg_replace("/([A-Z])/","_\\1",$name);
        //去除第一个_
        $str = strtolower(ltrim($str,"_"));
        $filedir = CMF_ROOT."public/themes/admin_simpleboot3/admin/".$str."/";
        Directory($filedir);
        $fileName = $filedir."edit.html";
        //开始替换
        ob_start();

        echo str_replace(array("#CONTROLLERNAME#","#replace_content#","#EDIT_PHOTO_JS#","#EDIT_FILES_JS#","#DESC#"),array($name,$html_str,$edit_photo_js,$edit_files_js,$desc),$content);
        $info2 = ob_get_contents();
        file_put_contents($fileName,$info2);
        ob_end_clean();
    }

    /**
     * 创建列表模版
     */
    public static function create_lists_tpl($model_info){
        new ListHtmlService($model_info);
    }



}